﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.User
{
    public class GroupUserBL
    {
        protected void clsGroupUserInsert(tbl_GroupUser groupusers)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_GroupUser groupuser = new tbl_GroupUser();
                    groupuser.GroupNo = groupusers.GroupNo;
                    groupuser.UserNo = groupusers.UserNo;
                    groupuser.CreatedDate = DateTime.Now;
                    groupuser.CreatedUser = groupusers.CreatedUser;
                    groupuser.Status = groupusers.Status;

                    adlerContextDb.tbl_GroupUsers.InsertOnSubmit(groupuser);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsGroupUserDelete(tbl_GroupUser groupusers)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var groupuser = adlerContextDb.tbl_GroupUsers.Single(d => d.GroupNo == groupusers.GroupNo && d.UserNo == groupusers.UserNo);
                    groupuser.Status = groupusers.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public List<tbl_GroupUser> GetGroupUser()
        {
            AdlerContextDataContext adlerContextDb=new AdlerContextDataContext();
            return adlerContextDb.tbl_GroupUsers.ToList();
        }
    }
}
